import Keycloak from 'keycloak-js';

console.log(process.env, REACT_APP_ENV);

const kc = new (Keycloak as any)(REACT_APP_ENV === 'dev' ? '/keycloak-dev.json' : '/keycloak.json');

export const keycloakInit = () => {
  return (
    kc
      .init({ onLoad: 'login-required', checkLoginIframe: false, promiseType: 'native' })
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      .then((authenticated: boolean) => {
        return new Promise((resolve) => {
          kc.updateToken(60)
            .then(() => {
              resolve(kc);
            })
            .catch(() => kc.logout());
        });
      })
  );
};

export const keycloakLogout = () => {
  kc.logout({
    redirectUri: `${window.location.protocol}//${window.location.hostname}:${window.location.port}`,
  });
};
